#!/bin/bash
# **********************************************************************
# piezometer data sending process functions
# Copyright (C) Muhammad Fauzilkamil Zainuddin <jerungkun@gmail.com>
# http://coderstalk.blogspot.com
#
# Description to send piezometer data to server without leaving any 
# unwanted zombie process
#
# **********************************************************************

# default variable settings
pmhost="apogeek.com/piezometer"	# default pisometer host address
sender="SAMPLE/PZ-SAMPLE/PIEZOMETER"	# default sender id or device id
debugmode=1                             # debug flag
logfilepath="/var/log/pmlog"			# log filename

# get config
. /etc/piezometer/piezometer.conf

# echo wrapper to avoid echo problem
echomsg () {
    inputstr=$@
    
    if [ "$debugmode" = 1 ]; then
        echo "$(date): $inputstr" >> $logfilepath
    fi
}

# --------------------------------------------------------
# function to send the http request to the database server
# --------------------------------------------------------
sendtickvalue () {
    #get the request string
    reqstring=$1
    outputfolder="/tmp/piezotmp"

    if [ ! -d "$outputfolder" ]; then
        mkdir -p "$outputfolder" >> /dev/null 2>&1
    fi
    
    outputreqfile="$outputfolder/$(date '+%y%m%d%H%M%S').tmp"

    # remove httprequest output file
    if [ -e "$outputreqfile" ]; then rm "$outputreqfile" >> /dev/null 2>&1; fi

    # do the request
    wget -cq -O $outputreqfile $reqstring >> /dev/null 2>&1

    # check the output file
    if [ -e "$outputreqfile" ]; then
        # check the value in the file
        outval=$(cat "$outputreqfile")
         
        if [ "$outval" = "DATASENT" ]; then
            # the query is succesfully sent to the asp/php page
            # return 1 for success 
            ret=1
        else
            # the query is not succesfully sent to the asp/php page
            # return 0 for fail
            ret=0 
        fi 
    else
        # the call fails
        # return 0 for fail
        ret=0
    fi
   
    # remove httprequest output file
    if [ -e "$outputreqfile" ]; then rm "$outputreqfile" >> /dev/null 2>&1; fi

    return $ret 
}

# --------------------------------------------------------
# function called by external script (/usr/bin/piezoproc)
# this function wait for two arguments
# /usr/bin/piezoproc 6332.22 25.22
#                        |     |
#                        |     +----- Temperature
#                        |     
#                        +---- Pressure (kPa)     
# --------------------------------------------------------
procpiezo() {
    pressure=$1
    temperat=$2
    
    echomsg "PROCPIEZO start $line"

    ticktime=$(date '+%Y/%m/%d,%H:%M:%S')
    
    httprequest="http://$pmhost/pzcollector.php?id=$sender&dt=$ticktime&p=$pressure&t=$temperat"

    # for debugging purpose
    echomsg $httprequest

    # call the send function
    sendtickvalue $httprequest

    # get the return value
    sent=$?
    if [ "$sent" = 1 ]; then
        # the message is sent
        # echomsg "message sent = $sent"
        echomsg "Message Successfully Sent"
    else
        # the message is not sent
        echomsg "Message Sending Failed"
    fi
    
    echomsg "PROCPIEZO end!"
}
